package util

import (
	"fmt"
	"strconv"
	"strings"
)

// ConvertConfigValue 在反序列化之后，你可以进行类型断言或检查
func ConvertConfigValue(value interface{}) (string, error) {
	switch v := value.(type) {
	case int:
		return strconv.Itoa(v), nil
	case int8:
		return strconv.FormatInt(int64(v), 10), nil
	case int16:
		return strconv.FormatInt(int64(v), 10), nil
	case int32:
		return strconv.FormatInt(int64(v), 10), nil
	case int64:
		return strconv.FormatInt(v, 10), nil
	case uint:
		return strconv.FormatUint(uint64(v), 10), nil
	case uint8:
		return strconv.FormatUint(uint64(v), 10), nil
	case uint16:
		return strconv.FormatUint(uint64(v), 10), nil
	case uint32:
		return strconv.FormatUint(uint64(v), 10), nil
	case uint64:
		return strconv.FormatUint(v, 10), nil
	case float32:
		return strconv.FormatFloat(float64(v), 'f', -1, 64), nil
	case float64:
		return strconv.FormatFloat(v, 'f', -1, 64), nil
	case string:
		return v, nil
	case bool:
		return strconv.FormatBool(v), nil
	default:
		return "", fmt.Errorf("unsupported type: %T", value)
	}
}

// Contains 判断字符是否在数组内
func Contains(elems []string, v string) bool {
	for _, s := range elems {
		if v == s {
			return true
		}
	}
	return false
}
func ArrToString(arr []string) string {
	// 将整数切片转换为字符串切片
	stringSlice := make([]string, len(arr))
	for i, num := range arr {
		stringSlice[i] = num
	}

	// 将字符串切片连接成一个单一的字符串
	result := strings.Join(stringSlice, ",")
	return result
}
func Int64ArrayToString(arr []int64) string {
	// 将整数切片转换为字符串切片
	stringSlice := make([]string, len(arr))
	for i, num := range arr {
		stringSlice[i] = Int64ToString(num)
	}

	// 将字符串切片连接成一个单一的字符串
	result := strings.Join(stringSlice, ",")
	return result
}
func StringToArr(str string) []string {
	// 使用逗号分隔字符串
	arr := strings.Split(str, ",")
	return arr
}
